package com.liulin.system.controller;

import com.liulin.system.entity.Student;
import com.liulin.system.service.StudentService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Student)表控制层
 *
 * @author makejava
 * @since 2021-06-22 19:16:56
 */
@RestController
@RequestMapping("/student")
@CrossOrigin
public class StudentController {
    /**
     * 服务对象
     */
    @Resource
    private StudentService studentService;

    /**
     * 通过主键查询单条数据
     *
     * @param sno 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    public Student selectOne(Integer sno) {
        return this.studentService.queryById(sno);
    }

    @GetMapping("/deleteOne")
    public Boolean deleteOne(Integer sno){
        try{
            return this.studentService.deleteById(sno);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }


    @GetMapping("/addOne")
    public Student addOne(String sname,String ssex,Integer sage,String sdept,String sclass,String stelephone){
        Student student=new Student();
        student.setSname(sname);
        student.setSsex(ssex);
        student.setSage(sage);
        student.setSdept(sdept);
        student.setSclass(sclass);
        student.setStelephone(stelephone);
        try {
            return this.studentService.insert(student);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }


    @GetMapping("/updateOne")
    public Student updateOne(Integer sno,String sname,String ssex,Integer sage,String sdept,String sclass,String stelephone){
        Student student=new Student();
        student.setSno(sno);
        student.setSname(sname);
        student.setSsex(ssex);
        student.setSage(sage);
        student.setSdept(sdept);
        student.setSclass(sclass);
        student.setStelephone(stelephone);
        try {
            return  this.studentService.update(student);
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
